package com.sphd.beautysalon.activitys.vip;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.sphd.beautysalon.fragments.TopupNoFragment;
import com.sphd.beautysalon.interfaces.VipNoCallBack;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.adapters.MyViewPagerAdapter;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.fragments.ComFragment;
import com.sphd.beautysalon.fragments.DisFragment;
import com.sphd.beautysalon.fragments.TopupFragment;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.utils.Constant;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 全部会员
 */
public class VipActivity extends BaseActivity implements VipNoCallBack {

    @BindView(R.id.spinner_viptype)
    Spinner spinnerViptype;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.my_vp)
    ViewPager myVp;
    List<Fragment> fragmentList;
    MyViewPagerAdapter myViewPagerAdapter;
    Intent intent;
    Bundle bundle;
    LoginInfo loginInfo;
    RestaurantInfo restaurantInfo;
    int TYPE = 1;
    String titles[] = {"", "", "",""};
    ComFragment comFragment;
    DisFragment disFragment;
    TopupFragment topupFragment;
    TopupNoFragment topupNoFragment;
    private static final String TAG = "VipActivity";
    @BindView(R.id.add_btn)
    ImageView addBtn;
    @BindView(R.id.search_btn)
    ImageView searchBtn;
    @BindView(R.id.tv_coon)
    TextView tvCoon;
    @BindView(R.id.tv_dis)
    TextView tvDis;
    @BindView(R.id.tv_topup)
    TextView tvTopup;
    List<TextView> tvs;
    @BindView(R.id.tv_topup_no)
    TextView tvTopupNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> finish());
        spinnerViptype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                TYPE = i;
                topupFragment.setVal(TYPE, restaurantInfo);
                comFragment.setVal(TYPE, restaurantInfo);
                disFragment.setVal(TYPE, restaurantInfo);
                topupNoFragment.setVal(TYPE, restaurantInfo);
                if (myVp.getCurrentItem() == 0) {
                    comFragment.onRefresh();
                } else {
                    comFragment.changeType();
                }
                if (myVp.getCurrentItem() == 1) {
                    disFragment.onRefresh();
                } else {
                    disFragment.changeType();
                }
                if (myVp.getCurrentItem() == 2) {

                    topupFragment.onRefresh();
                } else {
                    topupFragment.changeType();
                }if (myVp.getCurrentItem() == 3) {

                    topupNoFragment.onRefresh();
                } else {
                    topupNoFragment.changeType();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        myVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                chooseTv(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    private void chooseTv(int position) {
        for (int i = 0; i < tvs.size(); i++) {
            if (position == i) {
                tvs.get(i).setTextColor(getResources().getColor(R.color.indigo_500));
            } else {
                tvs.get(i).setTextColor(getResources().getColor(R.color.gray));
            }
        }
    }

    private void initView() {
        setTitleBar(toolbar, "");
        spinnerViptype.setSelection(TYPE, true);
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);

        tvs = new ArrayList<>();
        tvs.add(tvCoon);
        tvs.add(tvDis);
        tvs.add(tvTopup);
        tvs.add(tvTopupNo);

        fragmentList = new ArrayList<>();
        comFragment = new ComFragment();
        disFragment = new DisFragment();
        topupFragment = new TopupFragment();
        topupNoFragment = new TopupNoFragment();


        fragmentList.add(comFragment);
        fragmentList.add(disFragment);
        fragmentList.add(topupFragment);
        fragmentList.add(topupNoFragment);


        comFragment.setVipCallBack(this);

        disFragment.setVipCallBack(this);
        topupFragment.setVipCallBack(this);
        topupNoFragment.setVipCallBack(this);

        comFragment.setVal(TYPE, restaurantInfo);
        disFragment.setVal(TYPE, restaurantInfo);
        topupFragment.setVal(TYPE, restaurantInfo);
        topupNoFragment.setVal(TYPE, restaurantInfo);

        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragmentList, titles);
        myVp.setAdapter(myViewPagerAdapter);
        tabs.setupWithViewPager(myVp);
        if (!isReciverCanDo(loginInfo, restaurantInfo)) {
            addBtn.setVisibility(View.GONE);
        }
        myVp.setCurrentItem(0);
    }

    @Override
    public void recRes(int com, int dis, int topup, int topupno) {

        titles[0] = "" + com;
        titles[1] = "" + dis;
        titles[2] = "" + topup;
        titles[3] = "" + topupno;
        myViewPagerAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.add_btn, R.id.search_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_btn:
                intent.setClass(context, AddVipActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.search_btn:
                intent.setClass(context, SearchVipActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2) {
            Bundle bundle = data.getExtras();
            int type = bundle.getInt(Constant.TYPE);
            myVp.setCurrentItem(type);
            if (type == 0) {
                comFragment.onRefresh();
            } else if (type == 1) {
                disFragment.onRefresh();
            } else if (type == 2) {
                topupFragment.onRefresh();
            }else if (type==3){
                topupNoFragment.onRefresh();
            }
        }
    }
}
